package top.ljlgithub.api.controller;

import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.ljlgithub.api.bean.Cat;
import top.ljlgithub.api.bean.CatNameParam;
import top.ljlgithub.api.bean.CatParam1;
import top.ljlgithub.api.bean.TreeCat;
import top.ljlgithub.api.common.response.Meta;
import top.ljlgithub.api.common.response.ResponseEntity;
import top.ljlgithub.api.exception.ParamException;
import top.ljlgithub.api.service.CatService;
import top.ljlgithub.api.vo.CatParam;

import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/5/7 8:02
 * @Version 1.0
 **/
@CrossOrigin
@RestController
public class CatController {
    @Autowired
    CatService catService;

    @RequiresPermissions("user:getAllCategories:*")
    @GetMapping("/categories")
    public ResponseEntity getCateTree(CatParam1 catParam1) throws ParamException {
        CatParam catParam = new CatParam(catParam1.getType(), catParam1.getPageNum(), catParam1.getPageSize());
        //type参数值默认为3
        if (catParam.getType() == null){
            catParam.setType(3);
        }
        Meta meta = new Meta("获取成功",200);
        Object data = catService.getCateTree(catParam);
        ResponseEntity<Object> result = new ResponseEntity<>(data,meta);
        return result;
    }

    @RequiresPermissions("user:getCategoryById:*")
    @GetMapping("/categories/{id}")
    public ResponseEntity getCateById(@PathVariable("id") Integer id){
        Meta meta = new Meta("获取成功",200);
        Cat cat = catService.findCatById(id);
        ResponseEntity<Object> result = new ResponseEntity<>(cat,meta);
        return result;
    }

    @RequiresPermissions("user:deleteCategory:*")
    @DeleteMapping("/categories/{id}")
    public ResponseEntity deleteCateById(@PathVariable("id") Integer id){
        Meta meta = new Meta("删除成功",200);
        catService.deleteCateById(id);
        ResponseEntity<Object> result = new ResponseEntity<>(null,meta);
        return result;
    }


    @PutMapping("/categories/{id}")
    public ResponseEntity updateCate(@PathVariable("id") Integer id, @RequestBody CatNameParam catNameParam) throws ParamException {
        String cat_name = catNameParam.getCat_name();
        if (cat_name == null){
            throw new ParamException();
        }
        Meta meta = new Meta("更新成功",200);
        Cat cat = catService.updateCate(id, cat_name);
        ResponseEntity<Object> result = new ResponseEntity<>(cat,meta);
        return result;
    }

    @PostMapping("/categories")
    public ResponseEntity addCate(@RequestBody Cat cate) throws ParamException {
        if (cate.getCat_pid() == null || cate.getCat_name() == null || cate.getCat_level() == null){
            throw new ParamException("参数不合法");
        }
        Meta meta = new Meta("创建成功",201);
        Cat cat = catService.addCate(cate);
        ResponseEntity<Object> result = new ResponseEntity<>(cat,meta);
        return result;
    }


}
